﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ModifyTest_ModifyTestItem : System.Web.UI.Page
{
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTest();
            BindTestItem();
            TextBox1.Text = GridView2.DataKeys[0].Value.ToString();
            TextBox2.Text = (string)Session["productSN"];
            TextBox3.Text = Session["testTime"].ToString();
        }
    }

    protected void BindTestItem()
    {
        TestItem testItem = new TestItem();
        string str1 = (string)Session["productSN"];
        DateTime dt1 = (DateTime)Session["testTime"];
        if (str1 != null)
        {
            GridView1.DataSource = testItem.GetTestItems(str1, dt1);
            GridView1.DataBind();
        }
    }

    protected void BindTest()
    {
        Test test=new Test();
        string str1 = (string)Session["productSN"];
        DateTime dt1 = (DateTime)Session["testTime"];
        if(str1!=null)
        {
            IList<TestInfo> tests = new List<TestInfo>();
            tests.Add(test.GetTest(str1, dt1));
            GridView2.DataSource = tests;
            GridView2.DataBind();
        }
    }

    public void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView2.EditIndex = e.NewEditIndex;
        BindTest();
    }

    public void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TestInfo testInfo = new TestInfo();
        testInfo.TestID = (Guid)GridView2.DataKeys[e.RowIndex].Value;
        testInfo.ProductSN = (string)Session["productSN"];
        testInfo.TestTime = (DateTime)Session["testTime"];
        testInfo.ProductTypeID = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim().ToString();
        testInfo.UserID = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim().ToString();
        testInfo.ENAID = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim().ToString();
        testInfo.IsPass = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim().ToString();
        testInfo.IsSwitch = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[6].Controls[0])).Text.Trim().ToString();

        Test test = new Test();
        test.Update_SNDT(testInfo);
        GridView2.EditIndex = -1;
        BindTest();
    }

    protected void GridView2_RowCanncelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView2.EditIndex = -1;
        BindTest();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindTestItem();
    }

    public void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindTestItem();        
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //TestItemModifyLogInfo TIMLI = new TestItemModifyLogInfo();
        //TIMLI.TestItemID = GridView1.DataKeys[e.RowIndex].Value.ToString();
        //TIMLI.PreTestItemName = GridView1.Rows[e.OldValues[TestItemName]].ToString();
        //TIMLI.PreProductSN = GridView1.Rows[e.OldValues[ProductSN]].ToString();
        //TIMLI.PreTestTime = GridView1.Rows[e.OldValues[TestTime]].ToString();
        //TIMLI.PreTestTypeID = GridView1.Rows[e.OldValues[TestTypeID]].ToString();
        //TIMLI.PreStartF = GridView1.Rows[e.OldValues[StartF]].ToString();
        //TIMLI.PreStopF = GridView1.Rows[e.OldValues[StopF]].ToString();
        //TIMLI.PreMax = GridView1.Rows[e.OldValues[Max]].ToString();
        //TIMLI.PreMin = GridView1.Rows[e.OldValues[Min]].ToString();
        //TIMLI.PreHighSpec = GridView1.Rows[e.OldValues[HighSpec]].ToString();
        //TIMLI.PreLowSpec = GridView1.Rows[e.OldValues[LowSpec]].ToString();
        //TIMLI.PreIsPass = GridView1.Rows[e.OldValues[IsPass]].ToString();
        //TIMLI.PostTestItemName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.Trim().ToString();
        //TIMLI.PostProductSN = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();
        //TIMLI.PostTestTime = Convert.ToDateTime(((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text);
        //TIMLI.PostTestTypeID = Convert.ToInt16(((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text);
        //TIMLI.PostStartF = Convert.ToDecimal(((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text);
        //TIMLI.PostStopF = Convert.ToDecimal(((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text);
        //TIMLI.PostMax = Convert.ToDecimal(((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text);
        //TIMLI.PostMin = Convert.ToDecimal(((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text);
        //TIMLI.PostHighSpec = Convert.ToDecimal(((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text);
        //TIMLI.PostLowSpec = Convert.ToDecimal(((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Text);
        //TIMLI.PostIsPass = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[10].Controls[0])).Text.Trim().ToString();
        //TIMLI.ModifyUserID = Membership.GetUser().UserName.ToString();

        //TestItemModifyLog TIML = new TestItemModifyLog();
        //TIML.Insert(TIMLI);

        TestItemInfo testItem = new TestItemInfo();
        testItem.TestItemID = (Guid)GridView1.DataKeys[e.RowIndex].Value;
        testItem.TestItemName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.Trim().ToString();
        testItem.ProductSN = (string)Session["productSN"];
        testItem.TestTime = (DateTime)Session["testTime"];
        testItem.TestTypeID = Convert.ToInt16(((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text);
        testItem.StartF = Convert.ToDecimal(((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text);
        testItem.StopF = Convert.ToDecimal(((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text);
        testItem.Max = Convert.ToDecimal(((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text);
        testItem.Min = Convert.ToDecimal(((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text);
        testItem.HighSpec = Convert.ToDecimal(((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text);
        testItem.LowSpec = Convert.ToDecimal(((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Text);
        testItem.IsPass = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[10].Controls[0])).Text.Trim().ToString();
        testItem.TestTypeName = "";

        TestItem testitem = new TestItem();
        testitem.Update(testItem);        
        GridView1.EditIndex = -1;
        BindTestItem();
    }

    protected void GridView1_RowCanncelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindTestItem();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["TestID"] = (Guid)(GridView2.DataKeys[0].Value);

        Response.Redirect("~/ModifyTest/ModifyTest.aspx");
    }
}
